import {
  config
} from '../config.js'
const tips = {
  1: '抱歉,出错了',
  1005: '未授权',
  3000: '不存在'
}

class HTTP {
  request({
    url,
    data = {},
    method = "GET"
  }) {
    return new Promise((resolve, reject) => {
      this._request(url, resolve, reject, data, method)
    })
  }
  _request(url, resolve, reject, data = {}, method = "GET") { //必填参数必须在默认参数之前
    // url,data,method
    if (!method) {
      method = "GET"

      let arr = [];
      for (const key in data) {
        const ele = data[key];
        arr.push(ele)
      }
      var psString = JSON.stringify(arr);
      data = {
        ps: psString
      }
    }
    my.showLoading({
      content: '加载中',
    })
    const sessionId = getApp().globalData.sessionId
    const usrInfo = my.getStorageSync({
      key: 'userId'
    }).data
    if (usrInfo) {
      const openId = usrInfo.openId
      data = Object.assign({}, {
        openId: openId
      }, data)
    }
    if (my.canIUse('request')) {
      my.request({
        url: config.api_bais_url + url,
        data: data,
        method: method,
        headers: {
          'content-type': 'application/json', // 默认值
          'sessionId': sessionId
        },
        success: (res) => {
          my.hideLoading()
          const code = res.status.toString()
          if (code.startsWith(2)) {
            resolve(res.data.data)
            if (res.data.result != 200) {
              let result_msg = res.data.errmsg || res.data.message || res.data._error
              this._show_result(result_msg)
            }

          } else {
            console.log(res)
            reject()
            // let error_code = res.data.error_code
            // this._show_error(error_code)
          }
        },
        fail: (err) => {
          reject()
          this._show_error(1)
        }
      })

    } else {
      my.request({
        url: config.api_bais_url + url,
        data: data,
        method: method,
        headers: {
          'content-type': 'application/json', 
          // 'content-type': 'application/x-www-form-urlencoded', 
          'sessionId': sessionId
        },
        success: (res) => {
          my.hideLoading()
          const code = res.status.toString()
          if (code.startsWith(2)) {
            resolve(res.data.data)
            if (res.data.result != 200) {
              let result_msg = res.data.errmsg || res.data.message || res.data._error
              this._show_result(result_msg)
            }

          } else {
            console.log(res)
            reject()
            let error_code = res.data.error_code
            this._show_error(error_code)
          }
        },
        fail: (err) => {
          reject()
          this._show_error(1)
        }
      })
    }
  }
  _show_result(msg) {
    my.showToast({
      content: msg,
      icon: 'none',
      duration: 2000
    })
  }
  _show_error(error_code) {
    if (!error_code) {
      error_code = 1
    }
    const tip = tips[error_code]
    my.showToast({
      content: tip ? tip : tips[1],
      icon: 'none',
      duration: 2000
    })
  }
  _success(){
    
  }
}
export {
  HTTP
}